package com.atguigu.guli.service.edu.controller.api;


import com.atguigu.commonutils.JwtInfo;
import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.result.R;
import com.atguigu.guli.service.edu.entity.vo.CourseCollectVo;
import com.atguigu.guli.service.edu.service.CourseCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@Api(tags = "课程收藏列表")
@RestController
@RequestMapping("/api/edu/course-collect")
public class ApiCourseCollectController {

    @Autowired
    private CourseCollectService courseCollectService;

    @ApiOperation(value = "判断是否收藏")
    @GetMapping("auth/is-collect/{courseId}")
    public R isCollect(
            @ApiParam(name = "courseId", value = "课程id", required = true)
            @PathVariable String courseId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
       Boolean result = courseCollectService.isCollect(courseId,jwtInfo.getId());
       return R.ok().data("isCollect",result);
    }

    @ApiOperation(value = "收藏课程")
    @PostMapping("auth/save/{courseId}")
    public R save(
            @ApiParam(name = "courseId", value = "课程id", required = true)
            @PathVariable String courseId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
        courseCollectService.saveByCourseId(courseId,jwtInfo.getId());
        return R.ok();
    }

    @ApiOperation(value = "获取该用户的课程收藏列表")
    @GetMapping("auth/list")
    public R collectList(HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
       List<CourseCollectVo> courseCollectVoList = courseCollectService.collectList(jwtInfo.getId());
       return R.ok().data("items",courseCollectVoList);
    }
    @ApiOperation(value = "取消收藏课程")
    @DeleteMapping("auth/remove/{courseId}")
    public R remove(
            @ApiParam(name = "courseId", value = "课程id", required = true)
            @PathVariable String courseId,HttpServletRequest request

    ) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
      Boolean result =  courseCollectService.removeByCourseId(courseId,jwtInfo.getId());
        if (result) {
            return R.ok().message("课程收藏取消！");
        }else {
            return  R.error().message("课程收藏取消失败！");
        }
    }
}
